package com.qfedu.mtl.controller;


import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.Comments;
import com.qfedu.mtl.query.CommentQuery;
import com.qfedu.mtl.service.CommentService;
import com.qfedu.mtl.service.impl.CommentServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.page.Pager;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;
import com.qfedu.mtl.vo.TableVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/comment")
public class CommentController {

    private CommentService commentService = new CommentServiceImpl();

    @RequestMapping("/add.do")
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        Comments comments = HttpUtil.request2Bean(req, Comments.class);
        try {
            commentService.save(comments);
            HttpUtil.responseSuccess(comments, resp);
        } catch (Exception ex) {
            HttpUtil.response(null,-1, "失败 "+ex.getMessage(), resp);
        }
    }

    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String id = req.getParameter("commentId");
        if (StrUtil.isNullOrEmpty(id)) {
            HttpUtil.response(-1, null, "请指定删除ID", resp);
            return;
        }
        Comments comment = commentService.delete(Integer.valueOf(id));
        HttpUtil.responseSuccess(comment, resp);
    }

    @RequestMapping("/deleteBatch.do")
    public void deleteBatch(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String ids = req.getParameter("commentIds");
        if (StrUtil.isNullOrEmpty(ids)) {
            HttpUtil.response(-1, null, "请指定删除ID", resp);
            return;
        }
        String[] idss = ids.split(",");
        List<Integer>id= Stream.of(idss).map(e->Integer.valueOf(e)).collect(Collectors.toList());

        try {
            commentService.deleteBatch(id);
            HttpUtil.responseSuccess(null,resp);
        }catch (Exception ex) {
            HttpUtil.response(null,-1, "失败 "+ex.getMessage(), resp);

        }
    }

    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        Comments comments = HttpUtil.request2Bean(req, Comments.class);
        try {
            commentService.update(comments);
            HttpUtil.responseSuccess(comments, resp);
        } catch (Exception ex) {
            HttpUtil.response(null,-1, "失败 "+ex.getMessage(), resp);

        }
    }

    @RequestMapping("/find.do")
    public void find(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        String id = req.getParameter("commentId");
        Comments comment = commentService.findById(Integer.valueOf(id));
        HttpUtil.responseSuccess(comment, resp);

    }

    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        CharacterUtil.setContentWithJson(req, resp);
        //分页参数
        String page = StrUtil.ifNull(req.getParameter("page"), "1");
        String size = StrUtil.ifNull(req.getParameter("size"), "5");
        //查询条件
        CommentQuery query = HttpUtil.request2Bean(req, CommentQuery.class);
        //调用业务方法分页

        Pager<Comments> pager = commentService.findPage(Integer.valueOf(page), Integer.valueOf(size), query);

        TableVO vo = new TableVO();
        vo.setCode(0);
        vo.setMsg("");
        vo.setCount(pager.getTotal());
        vo.setData(pager.getRows());
        String json = JSON.toJSONString(vo);
        resp.getWriter().print(json);

    }

}
